import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        admin: {
            id: 0,
            userName: '',
            name: '',
            mobile: '',
            lastLogin: '',
            permissions: [], // 用户具备的权限，字符串数组，如['user.create','user.view','user.delete']
        },
        sysPagerHeader: {
            title: '',
            desc: '',
            breadcrumb: [] // 面包屑导航
        },
        includePageNames: [],
    },
    mutations: { // mutations和actions 有同步和异步的区别，建议在vue官网查看二者差异，一面被坑
        // 如果要设置admin的值就用 this.$store.commit('setAdmin',{ admin信息 })
        setAdmin(state,payload){
            state.admin = payload;
        },
        adminLogout (state) {
            localStorage.removeItem(window.consts.strings.adminToken);
            state.admin = {
                id: 0,
                userName: '',
                name:'',
                mobile: '',
                lastLogin: '',
                permissions:[],
            }
        },
        setSysPagerHeader (state, payload) {
            state.sysPagerHeader = payload
        },
        setIncludePageNames(state, payload) {
            state.includePageNames = payload
        },
    },
    actions: {
        // 使用 this.$store.dispatch('setUser',{...}) 触发
        setAdmin(context,payload){
            context.commit('setAdmin',payload);
        },
        adminLogout(context){
            context.commit('adminLogout')
        },
    },
    getters: {
        adminPermissions: state => {
            return state.admin.permissions;
        }
    },
    modules: {
        // 模板化是将不同板块的状态存储独立管理，一般项目没必要搞那么复杂
    }
})
